﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class UserDal
    {
        public UserDal(BabyfaceEntities _db)
        {
            db = _db;
        }
        public BabyfaceEntities db { get; set; }

        public int Add(TUser entity)
        {
            db.TUser.Add(entity);
            db.SaveChanges();

            return entity.TUserId;
        }

        public int Edit(TUser entity)
        {
            db.TUser.Attach(entity);
            db.Entry(entity).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public int Delete(int id)
        {
            var entity = new TUser { TUserId = id };
            db.TUser.Attach(entity);
            db.TUser.Remove(entity);
            //db.Entry(entity).State = EntityState.Deleted;
            return db.SaveChanges();
        }

        public TUser GetById(int id)
        {
            var entity = db.TUser.Where(w => w.TUserId == id).FirstOrDefault();
            return entity;
        }

        public IList<TUser> GetAll()
        {
            var list = db.TUser.ToList();
            return list;
        }

        public DataSet Login(string LoginName, string LoginPassword)
        {
            SqlParameter[] parms = {
                new SqlParameter("@LoginName",SqlDbType.VarChar,20),
                new SqlParameter("@LoginPassword",SqlDbType.VarChar,20)
            };
            parms[0].Value = LoginName;
            parms[1].Value = LoginPassword;
            //DataSet ds = db.Database.DataSet(CommandType.StoredProcedure, "PLogin", parms);
            return db.Database.DataSet(CommandType.StoredProcedure, "PLogin", parms);
        }
    }
}
